package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

/**
 * 学生数据操作类
 */
public class StudentDao implements BaseStudentDao{


    //创建数组
    private static Student[] stus = new Student[60];

    static{
        //初始化数据
        Student student1 = new Student("0001", "鹿晗", 28, "1992");
        Student student2 = new Student("0002", "吴亦凡", 30, "1990");
        Student student3 = new Student("0003", "蔡徐坤", 26, "1994");
        Student student4 = new Student("0004", "黄子韬", 25, "1995");
        stus[0]=student1;
        stus[1]=student2;
        stus[2]=student3;
        stus[3]=student4;
    }

    //实现添加学生操作
    public boolean addStudent(Student student) {

        //获取需要把数组存放的位置
        int index = -1;
        //遍历数组查看哪里是空的
        for (int i = 0; i < stus.length; i++) {
            //判断是否为空
            //stus[i]

            if (stus[i] == null) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            //返回放不下了
            return false;
        } else {
            //把学生放进去
            stus[index] = student;
            return true;
        }

    }

    public Student[] findAllStudents() {

        return stus;
    }

    //删除
    public void deleteStudent(String deleteId) {
        //通过学号查询数组中的位置
        int index = getIndex(deleteId);
        stus[index] = null;

    }

    //查询学号所在的位置
    public int getIndex(String deleteId) {
        for (int i = 0; i < stus.length; i++) {
            //stu[i]
            Student student = stus[i];
            if (student!=null&&deleteId.equals(student.getSid())) {
                return i;
            }
        }
        return -1;
    }

    public void updateStudent(String updateId, Student student) {
        int index = getIndex(updateId);
        stus[index] = student;

    }

    public void deleteStudentByIndex(int index) {
        stus[index] = null;

    }
}
